$(function () {
    vm.getDeptList();

    $("#jqGrid").jqGrid({
        url: baseURL + '/project/list',
        datatype: "json",
        colModel: [
            {label: '项目id', name: 'id', index: 'id', width: 50, key: true},
            {label: '项目名称', name: 'name', index: 'name', width: 80},
            {label: '所属公司', name: 'deptName', sortable: false, width: 80},
            {label: '创建时间', name: 'createTime', index: 'create_time', width: 80},
            {label: '修改时间', name: 'updateTime', index: 'update_time', width: 80}
        ],
        viewrecords: true,
        height: '100%',
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 50,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        },
        loadComplete: function (xhr) {
            if (xhr.code != 0) {
                alert(xhr.msg);
            }
        }
    });
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            projectName: '',
            deptId: ''
        },
        deptList: [],
        showList: true,
        title: null,
        project: {
            id: null,
            name: null,
            deptId: null,
            deptName: null
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.project = {
                id: null,
                name: null,
                deptId: null,
                deptName: null
            };

            vm.getDept();
        },

        getDept: function () {
            //加载部门树
            $.get(baseURL + "sys/dept/list", function (r) {
                ztree = $.fn.zTree.init($("#deptTree"), setting, r);
                var node = ztree.getNodeByParam("deptId", vm.project.deptId);
                console.log(node);
                if (node != null) {
                    ztree.selectNode(node);
                    vm.project.deptName = node.name;
                }
            })
        },

        deptTree: function () {
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择部门",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {

                    var node = ztree.getSelectedNodes();

                    vm.project.deptId = node[0].deptId;
                    vm.project.deptName = node[0].name;

                    console.log(vm.project.deptName);

                    layer.close(index);
                }
            });
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.project.id == null ? "project/save" : "project/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.project),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "project/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "project/info/" + id, function (r) {
                vm.project = r.project;
                vm.getDept();
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    "deptId": vm.q.deptId,
                    "projectName": vm.q.projectName
                },
                page: page
            }).trigger("reloadGrid");

        },
        reset: function () {
            vm.q.deptId = '';
            vm.q.projectName = '';
        },
        getDeptList: function () {
            $.get(baseURL + "sys/dept/list", function (r) {
                vm.deptList = r;
            });
        }
    }
});